package com.neuedu.autosale.cli.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.cli.entity.Client;
import com.neuedu.autosale.cli.service.ClientService;
import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;

@Controller
public class ClientController {
	@Autowired
	private ClientService clientService;
	
	@RequestMapping("client/index")
	private String clientIndex() {
		return "client/clientlist";
	}
	
	@RequestMapping("client/findall")
	@ResponseBody
	public List<Client> findAll(){
		return clientService.findAll();
	}
	
	@RequestMapping("client/list")
	@ResponseBody
	public Pager<Client> clientList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows){
		Pager<Client> pager=new Pager<>(page,rows);
		List<Client> clientList=clientService.findListByPager(pager);
		int total=clientService.findTotalByPager(pager);
		pager.setRows(clientList);
		pager.setTotal(total);
		return pager;
	}
	
	@RequestMapping("client/add")
	@ResponseBody
	public Result doAdd(Client client) {
		Result result=clientService.add(client);
		return result;
	}
	
	@RequestMapping("client/edit")
	@ResponseBody
	public Result doedit(Client client) {
		Result result=clientService.edit(client);
		return result;
	}
	
	@RequestMapping("client/delete")
	@ResponseBody
	public Result doDelete(Integer clientId) {
		List<Client> list=new ArrayList<Client>();
		list.add(new Client(clientId));
		Result result=clientService.deleteByIds(list);
		return result;
	}

}
